<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录</title>

    <!-- 加载静态文件 -->
    {% load static %}

    <!-- jQuery -->
    <script type="text/javascript" src="{% static 'js/jquery.min.js' %}"></script>

    <!-- Bootstrap -->
    <link rel="stylesheet" type="text/css" href="{% static 'bootstrap/css/bootstrap.min.css' %}">
    <script type="text/javascript" src="{% static 'bootstrap/js/bootstrap.min.js' %}"></script>

    <!-- jQuery Validator -->
    <script type="text/javascript" src="{% static 'jquery-validation/jquery.validate.min.js' %}"></script>
    <script type="text/javascript" src="{% static 'jquery-validation/localization/messages_zh.min.js' %}"></script>

    <!-- Layer -->
    <script type="text/javascript" src="{% static 'layer/layer.js' %}"></script>

    <!-- MD5.js -->
    <script type="text/javascript" src="{% static 'js/md5.min.js' %}"></script>

    <!-- Common.js -->
    <script type="text/javascript" src="{% static 'js/common.js' %}"></script>
</head>
<body>
<form name="loginForm" id="loginForm" method="post" style="width:50%; margin:0 auto">
    {% csrf_token %}

    <h2 style="text-align:center; margin-bottom: 20px">用户登录</h2>

    <div class="form-group">
        <div class="row">
            <label class="form-label col-md-4">请输入手机号码</label>
            <div class="col-md-5">
                <input id="mobile" name="mobile" class="form-control" type="text" placeholder="手机号码" required="true"/>
                <!-- 取消位数限制 -->
            </div>
            <div class="col-md-1">
            </div>
        </div>
    </div>

    <div class="form-group">
        <div class="row">
            <label class="form-label col-md-4">请输入密码</label>
            <div class="col-md-5">
                <input id="password" name="password" class="form-control" type="password" placeholder="密码"
                       required="true"/>
                <!-- 取消位数限制 -->
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-md-5">
            <button class="btn btn-primary btn-block" type="reset" onclick="reset()">重置</button>
        </div>
        <div class="col-md-5">
            <button class="btn btn-primary btn-block" type="submit" onclick="login()">登录</button>
        </div>
    </div>
</form>
</body>
<script>
    function login() {
        $("#loginForm").validate({
            submitHandler: function (form) {
                doLogin();
            }
        });
    }

    function doLogin() {
        g_showLoading();

        var inputPass = $("#password").val();
        var salt = $("#mobile").val();
        var str = "" + salt.charAt(0) + salt.charAt(2) + inputPass + salt.charAt(5) + salt.charAt(4);
        var password = md5(str);

        $.ajax({
            url: "/seckill_auth/login",
            type: "POST",
            data: {
                mobile: $("#mobile").val(),
                password: password,
                csrfmiddlewaretoken: '{{ csrf_token }}' // 添加 CSRF 令牌
            },
            success: function (data) {
                layer.closeAll();
                if (data.code == 200) {
                    layer.msg("成功");
                    console.log(data);
                    const accessToken = data.token; // 假设服务器返回的 Token 字段名为 access_token
                    document.cookie = `accessToken=${accessToken}; path=/; Secure`;// 将 Token 存储到 Cookie 中
                    window.location.href = "/goods/list";
                } else {
                    layer.msg(data.message);
                }
            },
            error: function () {
                layer.closeAll();
            }
        });
    }
</script>
</html>